﻿package com.snakedickgames.common.tweenmax 
{
    import com.greensock.TweenMax;
    import com.pblabs.engine.entity.EntityComponent;
        
    public class TweenMaxComponent extends EntityComponent
    {    
        public var fromVars:Object = null;
        public var toVars:Object = null;
        public var duration:Number = 1.0;
        public var target:*;
                
        public function set tween(value:TweenMax):void
        {
            _tween = value;
        }
        
        public function get tween():TweenMax
        {
            return _tween;
        }
        
        public function set enabled(value:Boolean):void
        {
            _enabled = value;
            if (_tween)
                _tween.setEnabled(value);
        }
        
        public function get enabled():Boolean 
        {
            return _enabled;
        }
        
        protected function _addEvents(obj:Object):void
        {
            obj["onCompleteListener"] = owner.eventDispatcher.dispatchEvent;
            // TODO: add other events here
        }
                
        override protected function onAdd():void 
        {
            if (toVars)
            {
                _addEvents(toVars);
                if (fromVars)
                {
                    tween = TweenMax.fromTo(target, duration, fromVars, toVars);
                }
                else
                {
                    tween = TweenMax.to(target, duration, toVars);
                }
            }
            else if (fromVars)
            {    
                _addEvents(fromVars);
                tween = TweenMax.from(target, duration, fromVars);
            }
        }
        
        override protected function onRemove():void 
        {
            tween = null;
        }
                
        protected var _tween:TweenMax;
        protected var _enabled:Boolean = true;
    }
}